package chat

import (
	"gxcvu-ai/app/model"
	"gxcvu-ai/pkg/database"
)

type Chat struct {
	model.BaseModal

	UserID   uint   `json:"user_id,omitempty"`
	Title    string `json:"title,omitempty"`
	MarketID uint   `json:"market_id,omitempty"`
	Content  []Message

	model.CommonTimestampsField
}

func CreateChat(data Chat) Chat {
	database.DB.Create(&data)
	return data
}

func GetChat(idstr string) (Chat, error) {
	var data Chat
	err := database.DB.
		Model(&Chat{}).
		Preload("Content").
		Where("id = ?", idstr).
		First(&data).Error
	return data, err
}

func GetAllChat(userId string) ([]Chat, error) {
	var chats []Chat
	err := database.DB.
		Model(&Chat{}).
		Preload("Content").
		Where("user_id = ?", userId).
		Find(&chats).
		Error

	return chats, err
}

func (chatModel *Chat) Save() (row int64) {
	result := database.DB.Save(&chatModel)
	return result.RowsAffected
}
